﻿Imports System.Net
Imports System.Net.Sockets

''' <summary>
''' 异步连接事件上下文
''' </summary>
Public Class AsyncClientToken
    Implements ICloneable
    ''' <summary>
    ''' 客户端ID
    ''' </summary>
    ''' <returns></returns>
    Public Property ClientID As String
    ''' <summary>
    ''' 本地监听端口
    ''' </summary>
    ''' <returns></returns>
    Public Property Local As EndPoint
    ''' <summary>
    ''' 远端监听端口
    ''' </summary>
    ''' <returns></returns>
    Public Property Remote As EndPoint
    ''' <summary>
    ''' 客户端Socket
    ''' </summary>
    ''' <returns></returns>
    Public Property Socket As Socket
    ''' <summary>
    ''' 连接接入时间
    ''' </summary>
    ''' <returns></returns>
    Public Property ConnectTime As DateTime
    ''' <summary>
    ''' 客户端消息
    ''' </summary>
    ''' <returns></returns>
    Public Property Buffer As BufferChunk


    ''' <summary>
    ''' 清空上下文对象
    ''' </summary>
    Protected Friend Sub Clear()
        Local = Nothing
        Remote = Nothing
        Socket = Nothing
        ConnectTime = DateTime.MinValue
        Buffer = Nothing
        ClientID = String.Empty
    End Sub



    ''' <summary>
    ''' 克隆一个
    ''' </summary>
    ''' <returns></returns>
    Public Function Clone() As Object Implements ICloneable.Clone
        Dim oClone As AsyncClientToken = Me.MemberwiseClone()
        oClone.Buffer = Nothing
        Return oClone
    End Function

    ''' <summary>
    ''' 设置Token内容
    ''' </summary>
    ''' <param name="sKey"></param>
    ''' <param name="oClientSocket"></param>
    Friend Sub SetToken(sKey As String, oClientSocket As Socket)
        ClientID = sKey
        Socket = oClientSocket
        ConnectTime = Now
        Remote = oClientSocket.RemoteEndPoint
        Local = oClientSocket.LocalEndPoint
    End Sub

    Public Overrides Function ToString() As String
        Return $"{ClientID}:{Remote}"
    End Function
End Class
